import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Utilities {
	
	//read a file from a specified path
	//return a String  
	public static String readFile(String inputFilePath){
		String inputString = "";
		try{
			FileReader reader = new FileReader(inputFilePath);
			BufferedReader bReader = new BufferedReader(reader);
			while(bReader.ready()){
				String line = bReader.readLine();
				//System.out.print(line+"\r\n");
				inputString += (line + '\r');
			}		
		}catch(IOException e){
			e.printStackTrace();
		}
		return inputString;
	}
	
	
	//write file to specified path, 
	//the content field refers to a String containing the content to be written
	public static void writeFile(String outputFilePath, String content){
	    
		try{
			FileWriter writer = new FileWriter(new File(outputFilePath));
			BufferedWriter bWriter = new BufferedWriter(writer);
			bWriter.write(content);
			writer.write(content);
			writer.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static String traverse(TreeNode root, int n) {
	    String output = "";
	    if(root == null)
            return "";
	    setType(root);
	    int i = n;
	    while(i-- > 0 ) {
	        System.out.print(".");
	        output += ".";
	    }
	    if(root.getType() == NodeType.ID) {
	        System.out.print("<ID:"+root.stringValue+">\r\n");
	        output += ("<ID:"+root.stringValue+">\r\n");
	    }
	    else if(root.getType() == NodeType.STR) {
	        System.out.print("<STR:'"+root.stringValue+"'>\r\n");
	        output += ("<STR:"+root.stringValue+">\r\n");
	    }
	    else if(root.getType() == NodeType.INT) {
            System.out.print("<INT:"+root.stringValue+">\r\n");
            output += ("<INT:"+root.stringValue+">\r\n");
	    }
	    else if (root.getType() == NodeType.YSTAR) {
	        System.out.print("<"+root.name+">\r\n");
	    }
	    else {
	        System.out.print(root.name + "\r\n");
	        output += root.name + "\r\n";
	    }
	    
        if (root.getExtraChild() != null) {
            output += traverseArray(root, n);
        } else {
            output += traverse(root.getLeftChild(), n + 1);
            output += traverse(root.getRightChild(), n + 1);
        }
        return output;
	}
	
    private static void setType(TreeNode t) {
        
        if(t.name.equals("neg")) {
            t.setType(NodeType.NEG);
        }else if(t.name.equals("not")) {
            t.setType(NodeType.NOT);
        }else if(t.name.equals("or")) {
            t.setType(NodeType.OR);
        }else if(t.name.equals("&")) {
            t.setType(NodeType.AND);
        }else if(t.name.equals("gr")) {
            t.setType(NodeType.GR);
        }else if(t.name.equals("ge")) {
            t.setType(NodeType.GE);
        }else if(t.name.equals("ls")) {
            t.setType(NodeType.LS);
        }else if(t.name.equals("le")) {
            t.setType(NodeType.LE);
        }else if(t.name.equals("eq")) {
            t.setType(NodeType.EQ);
        }else if(t.name.equals("ne")) {
            t.setType(NodeType.NE);
        }else if(t.name.equals("+")) {
            t.setType(NodeType.PLUS);
        }else if(t.name.equals("-")) {
            t.setType(NodeType.MINUS);
        }else if(t.name.equals("*")) {
            t.setType(NodeType.MULTI);
        }else if(t.name.equals("/")) {
            t.setType(NodeType.DIVIDE);
        }else if(t.name.equals("**")) {
            t.setType(NodeType.EXPO);
        }else if(t.name.equals("aug")) {
            t.setType(NodeType.AUG);
        }else if(t.name.equals("->")) {
            t.setType(NodeType.CONDITIONAL);
        }else if(t.name.equals("gamma")) {
            t.setType(NodeType.GAMMA);
        }else if(t.name.equals("lambda")) {
            t.setType(NodeType.LAMBDA);
        }else if(t.name.equals("tau")) {
            t.setType(NodeType.TAU);
        }else if(t.name.equals(",")) {
            t.setType(NodeType.COMMA);
        }else if(t.name.equals("=")) {
            t.setType(NodeType.EQUALS);
        }
        
     }
	
	private static String traverseArray(TreeNode temp, int n) {
	   	    String s = "";
	        s += traverse(temp.getExtraChild(),n+1);
	        if (temp.getLeftChild()!=null) 
	            s += traverseArray(temp.getLeftChild(),n);
	        return s;
	    
	}
}
